***reading in zipped men data, unziiping and retaining only the files I need
***https://u.osu.edu/odden.2/2013/09/26/use-stata-to-unzip-a-bunch-of-demographic-and-health-survey-files-and-put-them-where-i-want-them/

clear all
set more off

*******now reading them in to stata and appending 
****first men
clear all
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
set maxvar 31000
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
! ls *.DTA >mendata.txt


file open myfile using "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN/mendata.txt", read
file read myfile line
use `line'



save master_data_men, replace

file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	append using `line', keep(mv005  mv133     mv000 mv001 mv002 mv003 mv024 mv025  mv021 mcaseid mv013 ///
mv007 mv106 mv005   mv010 mv012  mv136  mv201 mv206 mv207  )
	file read myfile line
}
file close myfile
gen weight=mv005/1000000
svyset [pweight=weight], psu(mv021)

egen manid = group(mv000 mv001 mv002 mv003 mv024 mv025)


egen cluster = group(mv000 mv024 mv025 mv001)
egen clustername=concat(mv000 cluster)

keep  weight mv005  mv133    clustername  mv000 mv001 mv002 mv003 mv024 mv025  mcaseid mv013 ///
mv007 mv106 mv005   mv010 mv012  mv136  mv201 mv206 mv207 



foreach var of varlist mcaseid-clustername {
   	local newname = substr("`var'", 2, .)
   	rename `var' `newname'
}
gen Female=0
save master_data_men, replace

****now women -
clear all
set maxvar 31000
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/WOMEN"
! ls *.DTA >womendata.txt


file open myfile using "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/WOMEN/womendata.txt", read
file read myfile line
use `line'



save master_data_women, replace

file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	append using `line', keep  ( v133 v116  v000 v001 v002  v003  caseid v013 ///
v007 v106 v133 v005   v010 v012 v119-v130 v136   v201 v206 v207 v212   caseid v013 v113 v115  )

	file read myfile line
}
file close myfile
gen weight=v005/1000000
svyset [pweight=weight], psu(v021)
*drop if hw1>60



gen open_defac=.
replace open_defac=1 if v116==31
replace open_defac=0 if v116~=31 & v116~=.


egen cluster = group(v000 v024 v025 v001)

egen open_defac_by_psu1=mean(open_defac), by(cluster)

gen open_defac_by_psusq1=(open_defac_by_psu)^(2)

egen clustername=concat(v000 cluster)
keep   v133 v116   clustername  open_defac_by_psu1 open_defac_by_psusq1  v000 v001 v002 v003   caseid v013 ///
v007 v106 v133 v005  v191 v010 v012 v119-v130 v136 v201 v206 v207 v212  caseid v013 v113 v115 
gen Female=1

save master_data_women, replace
clear all
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/WOMEN"
use master_data_women




cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS/MEN"
append using master_data_men
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS"
save master_data_women_menv133, replace
clear all
cd "/Users/imac/Dropbox/Literacy_Analysis_DHS/RecodeDHS"

use master_data_women_menv133
split v000, p("2" "3" "4" "5" "6" "7" "8")
rename v0001 ccode
replace ccode="VN" if ccode=="VNT"
tab ccode
save master_data_women_menv133, replace
clear all
use master_data_women_menv133
sort ccode
merge m:1 ccode using country_codes_DHS
tab _merge
save master_data_women_menv133, replace
clear all
use master_data_women_menv133
gen notmatced=_merge
drop _merge
sort countryname
merge m:1 countryname using lang_policy
tab _merge
save master_data_women_menv133, replace
 
















